package com.springboot.framework.service.impl;

import com.github.binarywang.wxpay.bean.request.BaseWxPayRequest;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.service.WxPayService;
import com.springboot.framework.exception.BusinessException;
import com.springboot.framework.model.OrderModel;
import com.springboot.framework.utils.DateUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author SWF
 * @date 2019/6/18 14:39
 **/
@Service
public class PayServiceImpl {

    private static final String ORDER_BODY = "来一桶水-订单支付";

    @Resource
    private WxPayService wxPayService;

    public Object create(OrderModel orderModel) {
        try {
            WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
            orderRequest.setTradeType("JSAPI");
            orderRequest.setBody(ORDER_BODY);
            orderRequest.setOutTradeNo(orderModel.getId()); // 订单号
            orderRequest.setTotalFee(BaseWxPayRequest.yuanToFen(orderModel.getPayableAmount().toString()));//元转成分
            orderRequest.setOpenid(orderModel.getUserWechatOpenid());// openId
            orderRequest.setSpbillCreateIp("userIp");
            orderRequest.setTimeStart(DateUtil.format(orderModel.getCreateDate(),"yyyyMMddHHmmss")); // 开始时间
            orderRequest.setTimeExpire("yyyyMMddHHmmss"); // 结束时间

            return wxPayService.createOrder(orderRequest);
        } catch (Exception e) {
            //log.error("微信支付失败！订单号：{},原因:{}", orderNo, e.getMessage());
            e.printStackTrace();
            throw new BusinessException("支付失败，请稍后重试");
        }
    }

    public void notify1(){
        // 判断订单是否存在
        // 判断金额是否对
        // 判断支付状态是否正确
        // 修改支付状态
    }

    // 退款
    public void refund(OrderModel orderModel){

    }
}
